package com.zzyl.nursing.service.impl;


import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

// AppID(小程序ID)wxcc77ee7eac1e9d06
// AppSecret(小程序密钥)685e026bf16698929900e49064d8f36a
@Service
public class WechatServiceImpl implements IWechatService {

    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.appsecret}")
    private String appsecret;


    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    @Override
    public String getOpenid(String code) {
        System.out.println(appid);
        System.out.println(appsecret);
         // ?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

        Map<String,Object> params = new HashMap<>();
        params.put("appid",appid);
        params.put("secret",appsecret);
        params.put("js_code",code);
        params.put("grant_type","authorization_code");


        HttpResponse response = HttpUtil.createRequest(Method.GET, OPENID_URL)
                .form(params)
                .execute();

        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            return jsonObject.getStr("openid");
        }
        return null;
    }

    @Override
    public String getPhone(String phoneCode) {

        Map<String,Object> params = new HashMap<>();
        params.put("code",phoneCode);


        HttpResponse response = HttpUtil.createRequest(Method.POST, PHONE_URL+getToken())
                .body(JSONUtil.toJsonStr(params))
                .execute();

        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            // JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
            // String phoneNumber = phoneInfo.getStr("phoneNumber");
            String phoneNumber = jsonObject.getByPath("phone_info.phoneNumber",String.class);

            return phoneNumber;
        }
        return null;


    }

    private String getToken() {  //为了获取手机号

        System.out.println(appid);
        System.out.println(appsecret);
        // ?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

        Map<String,Object> params = new HashMap<>();
        params.put("appid",appid);
        params.put("secret",appsecret);
        params.put("grant_type","client_credential");


        HttpResponse response = HttpUtil.createRequest(Method.GET, TOKEN_URL)
                .form(params)
                .execute();

        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            return jsonObject.getStr("access_token");
        }
        return null;

    }
}
